

let State = require('./state');

// 编辑 桥洞
class EditHoleBridge extends State {

    onEnter() {
        super.onEnter();
        this.hideDeco();
        this.showBattery();
    }

    touchBegin(event) {

        let mapScene = editor.gameApp;
        let wpos = event.getLocation();
        let pos = mapScene.node.convertToNodeSpaceAR(wpos);

        // 判断是否选中
        let lstDeco = editor.getDecoView().getModel();

        // 只可以选中几个东西
        // 1. 洞
        // 2. 桥
        // 3. battery
        let selectedSp = null;
        for (let i = 0, len = lstDeco.length; i < len; ++i) {
            let sp = lstDeco[i];
            if (sp.isBattery() || sp.hole) {
                // 其实是需要将 pos 转换到 localPos
                // let localPos = sp.node.convertToNodeSpaceAR(pos);
                if (G.eui.containPos(sp.node,pos)) {
                    selectedSp = sp;
                    break;
                }
            }
        }

        if (selectedSp) {
            editor.gameApp.selectDeco(selectedSp);
            this._offx = pos.x - selectedSp.node.x;
            this._offy = pos.y - selectedSp.node.y;
        } else {
            editor.gameApp.selectDeco(null);
        }
    }

    touchMove(event) {
        let mapScene = editor.gameApp;
        let wpos = event.getLocation();
        let pos = mapScene.node.convertToNodeSpaceAR(wpos);

        let selectedSprite = editor.getInspectView().getSelectedSp();
        if (selectedSprite) {
            selectedSprite.node.setPosition(pos.x - this._offx, pos.y - this._offy);
            editor.getInspectView().updateInspectView(selectedSprite);
        }
    }
}


module.exports = EditHoleBridge;